const express = require('express');
const path = require('path');
const request = require('request');
const os = require('os');
const cors = require('cors');



const app = express();
// 代理请求的IP 换成后端提供的接口地址
let serverUrl='https://api.apiopen.top';

//let serverUrl='https://blog.csdn.net';

app.use(cors());

// 配置静态文件服务中间件
app.use(express.static(path.join(__dirname, 'public')));

let headers = {
  "user-agent": "Chrome/94.0.4606.71"
}

app.use('/', function(req, res) {
  let url = serverUrl + req.url;
  req.pipe(request(url)).pipe(res);
});

///获取本机ip///
function getIPAdress() {
    var interfaces = os.networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
}
const myHost = getIPAdress();

/* npm start 启动， 然后把 axios 请求的基地址 改为 127.0.0.1:4000 
   大功告成 ， 完美解决跨域问题
*/
app.listen(4000,function () {
  console.log( myHost + ':4000');
});
